package com.android.sama;

import java.util.ArrayList;
import java.util.HashMap;

import WebService.ServiceCommunicate;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class setting extends Activity {

	EditText et;
	Button bt;

	@Override
	
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting);
		et=(EditText)findViewById(R.id.server_url);
		TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
		bt=(Button)findViewById(R.id.connect);
		final String Imei = tm.getDeviceId();
		et.setText(loadprefs());
		
		final AlertDialog.Builder builder = new AlertDialog.Builder(this);

		bt.setOnClickListener(new OnClickListener() {
		ServiceCommunicate pd=new ServiceCommunicate();
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				String server_url=et.getText().toString();
				//String returns = ServiceCommunicate.checkDevice(Imei,server_url);
				
				if(!pd.checkDevice(Imei,server_url))
						{
						builder.setMessage("Server connection failed  \n or \n Device not registed");
					    AlertDialog alert = builder.create();
						alert.show();
						}
				else 
				{
					savePrefs("SERVER_URL",server_url);
					et.setText(server_url);
					builder.setMessage("Connect OK!");
				    AlertDialog alert = builder.create();
					alert.show();
				}
			}
		});
	
	}


	public   String loadprefs()
	{
		SharedPreferences save=PreferenceManager.getDefaultSharedPreferences(this);
		String url=save.getString("SERVER_URL","");
		if(url!=null)
		return url;
		else
			return "";
		
	} 
	void savePrefs(String pref_key,String pref_value)
	{
		SharedPreferences save=PreferenceManager.getDefaultSharedPreferences(this);
		Editor ed=save.edit();
		ed.putString(pref_key, pref_value);
		ed.commit();
		
	}
	void savePrefs(String pref_key,Boolean pref_value)
	{
		SharedPreferences save=PreferenceManager.getDefaultSharedPreferences(this);
		Editor ed=save.edit();
		ed.putBoolean(pref_key, pref_value);
		ed.commit();
	}
	

}
